package io.github.talelin.latticy.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.UserUtil;
import io.github.talelin.latticy.entity.SendCollect;
import io.github.talelin.latticy.mapper.SendCollectMapper;
import io.github.talelin.latticy.model.SendCollectDO;
import io.github.talelin.latticy.model.SendCollectDetailDO;
import io.github.talelin.latticy.repository.SendCollectRepository;
import org.hibernate.annotations.Where;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class SendCollectService extends ServiceImpl<SendCollectMapper, SendCollectDO>
{

    @Autowired
    private SendCollectRepository sendCollectRepository;

    @Transactional
    public SendCollect getDetail(Long id)
    {
        SendCollect sendCollect = this.get(id);
        //收花人查看
        Long collectUserId = UserUtil.getUserId();
        if (collectUserId == sendCollect.getCollectUserId() && sendCollect.getStatus() == 0) {
            sendCollect.setStatus(1);
            sendCollectRepository.save(sendCollect);
        }
        return sendCollect;
    }

    @Where(clause = "delete_time is null ")
    public SendCollect get(Long id)
    {
        Optional<SendCollect> sendCollect = sendCollectRepository.findById(id);
        return sendCollect.orElseThrow(() -> new NotFoundException());
    }


    /******************************* 后台 *************************************/
    public SendCollectDetailDO getSendCollectDetail(Long id)
    {
        SendCollectDetailDO sendCollectDO = this.getBaseMapper().getSendCollect(id);
        if (sendCollectDO == null) {
            throw new NotFoundException();
        }
        return sendCollectDO;
    }

    public void delete(Long id)
    {
        SendCollectDO sendCollectDO = this.getById(id);
        if (sendCollectDO == null) {
            throw new NotFoundException();
        }
        this.getBaseMapper().deleteById(id);
    }



}
